package com.unicom.room.controller;

import com.unicom.room.common.JsonResult;
import com.unicom.room.model.CheckInVo;
import com.unicom.room.model.CustomerVo;
import com.unicom.room.model.RoomStatus;
import com.unicom.room.service.CheckInService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
*  酒店订单管理控制器
*  @author 卢文
 */

@RestController
@RequestMapping("/hotelMgmt/checkInMgmt")
public class CheckInController {
	
	@Autowired
	private CheckInService checkInService;
	
	private static final Logger logger = LoggerFactory.getLogger(CheckInController.class);
	
	/**
	 * 查询当前房间开房情况
	 * @return
	 */
	@ApiOperation(value = "查询当前开房情况", notes = " 查询当前开房情况")
	@RequestMapping(value = "/listCheckIn", method = RequestMethod.GET)
	public JsonResult listCheckIn() {
		List<CheckInVo> data;
		try {
			data = checkInService.listCheckIn();
		} catch (Exception e) {
			logger.error("查询当前房间开房情况异常", e);
			return new JsonResult("500");
		}
		return new JsonResult("00000", data);
	}

	/**
	 * 开房
	 * @param customer
	 * @return
	 */
	@ApiOperation(value = "开房控制器", notes = "开房控制器")
	@RequestMapping(value = "/insertCustomer", method = RequestMethod.POST)
	public JsonResult insertCustomer(@RequestBody CustomerVo customer) {
		try {
			customer.setStatus((short)1);
			checkInService.insertCustomer(customer);
			return new JsonResult("00000");
		} catch (Exception e) {
			logger.error("保存开房记录异常", e);
			return new JsonResult("500");
		}
	}
	
	/**
	 * 退房
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "退房控制器", notes = " 退房控制器")
	@RequestMapping(value = "/deleteCheckIn/{id}", method = RequestMethod.DELETE)
	public JsonResult deleteCheckIn(@PathVariable("id") int id) {
		try {
			checkInService.deleteCheckIn(id);
			return new JsonResult("00000");
		} catch (Exception e) {
			logger.error("退房异常", e);
			return new JsonResult("500");
		}
	}
	
	/**
	 * 查询可换房间
	 * @param currentRoomNo
	 * @return
	 */
	@ApiOperation(value = "查询可换房间", notes = " 查询可换房间")
	@RequestMapping(value = "/getFreeRoom/{currentRoomNo}", method = RequestMethod.GET)
	public JsonResult getFreeRoom(@PathVariable("currentRoomNo") String currentRoomNo) {
		try {
			return new JsonResult("00000", checkInService.getFreeRoom(currentRoomNo));
		} catch (Exception e) {
			logger.error("查询可换房间异常", e);
			return new JsonResult("500");
		}
	}
	
	/**
	 * 换房
	 * @param roomNo
	 * @return
	 */
	@ApiOperation(value = "换房控制器", notes = " 换房控制器")
	@RequestMapping(value = "/changRoom/{id}/{roomNo}", method = RequestMethod.PUT)
	public JsonResult changRoom(@PathVariable("id") int id ,@PathVariable("roomNo") String roomNo) {
		try {
			checkInService.changRoom(id, roomNo);
			return new JsonResult("00000");
		} catch (Exception e) {
			logger.error("换房间异常", e);
			return new JsonResult("500", null, e.getMessage());
		}
	}
    /**
     * 查询所有房间状态
     * @return
     */
    @ApiOperation(value = "查询所有房间状态", notes = "查询所有房间状态")
    @RequestMapping(value = "/getallroomstatus", method = RequestMethod.GET)
    public JsonResult getAllRoomStatus() {
        List<RoomStatus> list = new ArrayList();
        try {
            list = checkInService.getAllRoomStatus();
        } catch (Exception e) {
            logger.error("查询所有房间状态异常", e);
            return new JsonResult("500");
        }
        return new JsonResult("00000", list);
    }
	
}
